package com.corpgovernment.organization.service.impl;

import com.corpgovernment.api.operatelog.chooseconstant.ChooseOperationType;
import com.corpgovernment.api.operatelog.vo.OperateLogContentDataVo;
import com.corpgovernment.api.organization.model.user.Find12306RequestBo;
import com.corpgovernment.common.enums.DeleteStatusEnum;
import com.corpgovernment.organization.constant.CommonConst;
import com.corpgovernment.organization.constant.OrgEmployeeConst;
import com.corpgovernment.organization.entity.db.User12306Relation;
import com.corpgovernment.organization.mapper.MbUser12306RelationMapper;
import com.corpgovernment.organization.service.IUser12306RelationService;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author wangyujue
 */
@Service
@Slf4j
public class User12306RelationServiceImpl extends OrganizationBaseService implements IUser12306RelationService {
    @Autowired
    private MbUser12306RelationMapper mbUser12306RelationMapper;

    @Override
    public boolean saveUser12306Account(String uid, String account, String supplierCode, String accountPwd) {
        //先删后增
        Example example = new Example(User12306Relation.class);
        example.createCriteria().andEqualTo("uid",uid).andEqualTo("supplierCode", supplierCode).andEqualTo("isDeleted", DeleteStatusEnum.F.getCode());
        mbUser12306RelationMapper.deleteByExample(example);

        User12306Relation user12306Relation = new User12306Relation();
        user12306Relation.setUid(uid);
        user12306Relation.setAccount(account);
        user12306Relation.setAccountPwd(accountPwd);
        user12306Relation.setSupplierCode(supplierCode);
        int result = mbUser12306RelationMapper.insertSelective(user12306Relation);
        return result > 0;
    }

    @Override
    public boolean updateUser12306Account(String uid, String account) {
        try {
            Example example = new Example(User12306Relation.class);
            example.createCriteria().andEqualTo(OrgEmployeeConst.UID, uid)
            .andEqualTo(CommonConst.IS_DELETED, "0");
            List<User12306Relation> result = mbUser12306RelationMapper.selectByExample(example);
            if (CollectionUtils.isNotEmpty(result)) {
                User12306Relation oldValue = result.get(0);
                List<OperateLogContentDataVo> operateLogContentDataVos = Lists.newArrayList(new OperateLogContentDataVo("12306账户", oldValue.getAccount(), account, ChooseOperationType.UPDATE));
                updateOperateLog(String.valueOf(oldValue.getId()), operateLogContentDataVos);
            }

        } catch (Exception e) {
            log.error("更新操作获取查询失败，User12306RelationServiceImpl.updateUser12306Account");
        }
        int i = mbUser12306RelationMapper.updateUser12306Account(uid, account);
        return i > 0;
    }

    @Override
    public User12306Relation findUser12306Account(Find12306RequestBo request) {
        Example example = new Example(User12306Relation.class);
        example.createCriteria().andEqualTo("uid",request.getUid()).andEqualTo("supplierCode", request.getSupplierCode()).andEqualTo("isDeleted", DeleteStatusEnum.F.getCode());
        List<User12306Relation> user12306Relations = mbUser12306RelationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(user12306Relations)){
            return null;
        }
        return user12306Relations.get(0);
    }

    @Override
    public boolean clearUser12306Account(String uid, String supplierCode) {
        int i = mbUser12306RelationMapper.deleteUser12306Account(uid, supplierCode);
        deleteOperateLog(uid);
        return i > 0;
    }
}
